//
//  ArrayDataSource.m
//  27-ArrayDataSource
//
//  Created by 曹曹 on 16/8/19.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ArrayDataSource.h"

@interface ArrayDataSource ()

// 声明初始化参数中对应的属性
@property (nonatomic, strong) NSArray *dataSourceArray;
@property (nonatomic, copy) NSString *reuse;
@property (nonatomic, copy) ConfigCell configCell;

@end

@implementation ArrayDataSource

- (instancetype)initWithTableViewCellReuse:(NSString *)reuse arrayDataSource:(NSArray *)array configCell:(ConfigCell)configCell {
    self = [super init];
    if (self) {
        // 属性赋值
        _dataSourceArray = array;
        _reuse = reuse;
        _configCell = configCell;
    }
    return self;
}

+ (instancetype)arrayDataSourceWithTableViewCellReuse:(NSString *)reuse arrayDataSource:(NSArray *)array configCell:(ConfigCell)configCell {
    return [[self alloc] initWithTableViewCellReuse:reuse arrayDataSource:array configCell:configCell];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_reuse];
    cell.textLabel.text = _dataSourceArray[indexPath.row];
    id model = _dataSourceArray[indexPath.row];
    // 根据数据源和cell调用block
    self.configCell(cell, model);
    return cell;
}
@end
